// 数据库初始化
const mysql = require("mysql");
const connection = mysql.createConnection({
  host: "localhost",
  user: "root",
  password: "root",
  database: "graduation"
});

connection.connect();
// 数据库调用函数对象
const bd = {
  //查询单条
  selectOne: ({ id, success = () => {}, fail = () => {} }) => {
    if (id > 0) {
      connection.query(`select * from hero where id = ${+id};`, function(
        error,
        results,
        fields
      ) {
        //失败返回
        if (error) throw error;
        // console.log(results);
        //判断成功失败
        if (results.length == 1) {
          success(results);
        } else {
          fail(error);
        }
      });
    } else {
      fail();
    }
  },
  //查询全部
  selectAll: ({ success = () => {}, fail = () => {} }) => {
    connection.query("select * from hero;", function(error, results, fields) {
      //失败返回
      if (error) throw error;
      //   console.log(results);
      //判断成功失败
      if (results.length >= 1) {
        success(results);
      } else {
        fail(error);
      }
    });
  },
  //增加信息
  add: ({
    name,
    skill,
    tale,
    wallpaper,
    success = () => {},
    fail = () => {}
  }) => {
    if (
      name &&
      skill &&
      tale &&
      wallpaper &&
      name != "" &&
      skill != "" &&
      tale != "" &&
      wallpaper != ""
    ) {
      connection.query(
        `insert into hero (name,skill,tale,wallpaper) values ("${name}","${skill}","${tale}","${wallpaper}");`,
        function(error, results, fields) {
          //失败返回
          if (error) throw error;
          //判断成功失败
          if (results.affectedRows == 1) {
            success(results);
          } else {
            fail(error);
          }
        }
      );
    } else {
      fail();
    }
  },
  //编辑信息
  edit: ({
    id,
    name,
    skill,
    tale,
    wallpaper,
    success = () => {},
    fail = () => {}
  }) => {
    if (
      name &&
      skill &&
      tale &&
      wallpaper &&
      name != "" &&
      skill != "" &&
      tale != "" &&
      wallpaper != "" &&
      id > 0
    ) {
      connection.query(
        `update hero set name="${name}" , skill="${skill}" , tale="${tale}" ,wallpaper="${wallpaper}"  where id=${+id};`,
        function(error, results, fields) {
          //失败返回
          if (error) throw error;
          console.log(results.affectedRows);

          //判断成功失败
          if (results.affectedRows == 1) {
            success(results);
          } else {
            fail(error);
          }
        }
      );
    } else {
      fail();
    }
  },
  //删除单条
  del: ({ id, success = () => {}, fail = () => {} }) => {
    if (id > 0) {
      connection.query(`delete from hero where id=${+id};`, function(
        error,
        results,
        fields
      ) {
        //失败返回
        if (error) throw error;
        // console.log(results);
        //判断成功失败
        if (results.length == 1) {
          success(results);
        } else {
          fail(error);
        }
      });
    } else {
      fail();
    }
  }
};
// 导出
module.exports = bd;
